#include "mq_msg.h"
#include <iostream>
#include <fcntl.h>           // For O_* constants
#include <sys/stat.h>        // For mode constants

//const char* QUEUE_NAME = "/my_message_queue";
const int MAX_MSG_SIZE = 1024;


MqMsgSend::MqMsgSend(std::string mq_name)
{
    __mq_name = mq_name;
    // init
    Init();
}

MqMsgSend::~MqMsgSend()
{
    // 开始销毁消息队列
    std::cout << "开始销毁消息队列" << std::endl;

    // 关闭消息队列
    mq_close(mq);

    // 删除消息队列（可选）
    //mq_unlink(__mq_name.c_str());
}


int MqMsgSend::Init()
{
    std::cout << "MqMsgSend::Init() create mqueue: " << __mq_name << std::endl;
    // 创建消息队列
    mq = mq_open(__mq_name.c_str(), O_CREAT | O_RDWR, 0666, nullptr);
    if (mq == -1) {
        perror("mq_open");
        return 1;
    }

    // 获取当前属性
    struct mq_attr attr;
    if (mq_getattr(mq, &attr) == -1) {
        perror("mq_getattr");
        mq_close(mq);
        return 1;
    }

    // 切换到非阻塞通信阶段
    std::cout << "Switching to non-blocking mode." << std::endl;
    attr.mq_flags |= O_NONBLOCK;
    if (mq_setattr(mq, &attr, nullptr) == -1) {
        perror("mq_setattr");
        mq_close(mq);
        return 1;
    }
}

int MqMsgSend::Send(std::string msg)
{
    // 发送消息到消息队列（非阻塞）
    if (mq_send(mq, msg.c_str(), msg.length() + 1, 0) == -1) {
        if (errno == EAGAIN) {
            std::cout << "Queue is full. Cannot send message in non-blocking mode." << std::endl;
        } else {
            perror("mq_send in non-blocking mode");
        }
        return -1;
    }

    std::cout << "mq send success: " << msg << std::endl;

    return 0;
}





//
//
//
//
//
//
//
//
//
//

MqMsgRecv::MqMsgRecv()
{

}

MqMsgRecv::MqMsgRecv(std::string mq_name)
{
    __mq_name = mq_name;
    // init
    Init();
}

MqMsgRecv::~MqMsgRecv()
{
    // 开始销毁消息队列
    std::cout << "开始销毁消息队列" << std::endl;

    // 关闭消息队列
    mq_close(mq);

    // 删除消息队列（可选）
    //mq_unlink(__mq_name.c_str());
}


int MqMsgRecv::InitWithName(std::string mq_name)
{
    __mq_name = mq_name;
    return Init();
}

int MqMsgRecv::Init()
{
    // init
    int rc;
    struct mq_attr mqAttr;

    printf ("Bringing up server.\n");
    rc = mq_unlink(__mq_name.c_str());
    if (rc < 0) {
        printf ("Warning on server mq_unlink.\n");
    }

    mqAttr.mq_maxmsg = 10;
    mqAttr.mq_msgsize = 1024;

    std::cout << "MqMsgRecv::Init() create mqueue: " << __mq_name << std::endl;
    // 创建消息队列
    mq = mq_open(__mq_name.c_str(), O_CREAT | O_RDWR, S_IWUSR|S_IRUSR, &mqAttr);
    if (mq == -1) {
        perror("mq_open");
        return 1;
    }

    // 获取当前属性
    struct mq_attr attr;
    if (mq_getattr(mq, &attr) == -1) {
        perror("mq_getattr");
        mq_close(mq);
        return 1;
    }

    // 切换到非阻塞通信阶段
    attr.mq_flags |= O_NONBLOCK;
    if (mq_setattr(mq, &attr, nullptr) == -1) {
        perror("mq_setattr");
        mq_close(mq);
        return 1;
    }
    return 0;
}

std::string MqMsgRecv::Recv()
{
    // 尝试从消息队列读取消息（非阻塞）
    char buffer[MAX_MSG_SIZE];
    size_t bytesRead = mq_receive(mq, buffer, MAX_MSG_SIZE, nullptr);
    if (bytesRead == -1) {
        if (errno == EAGAIN) {
            //std::cout << "No message available in the queue in non-blocking mode." << std::endl;
        } else {
            perror("mq_receive in non-blocking mode");
        }
        return "";
    } else {
        std::cout << "[MqMsgRecv::Recv] recv buffer:" << buffer << std::endl;
        return buffer;
    }
}